import { GameObject } from '@/assets/GameObject.js'
import { PlayerBase } from '@/assets/player/PlayerBase.js'

/**
 * 游戏地图基类，继承自GameObject
 * 负责管理游戏地图的基本功能、绘制和更新
 */
export class GameMapBase extends GameObject {
    /**
     * 构造函数，初始化游戏地图
     * @param {CanvasRenderingContext2D} ctx - 画布2D渲染上下文
     */
    constructor(ctx) {
        super()
        this.ctx = ctx
        this.width = this.ctx.canvas.width
        this.height = this.ctx.canvas.height

        // 游戏显示设置
        this.aspectRatio = { width: 16, height: 9 }

        this.players = []

        // 玩家参数比例
        this.sizeConfig = {
            playerRadius: 0.05, // 玩家半径占画面高度的比例
            playerSpeed: 0.15, // 玩家速度占画面高度的比例
        }

        this.resize()
    }

    // 调整窗口大小执行
    resize() {
        const canvas = this.ctx.canvas
        const oldWidth = this.width
        const oldHeight = this.height

        // 获取父容器尺寸
        const parent = canvas.parentElement
        const parentWidth = parent.clientWidth
        const parentHeight = parent.clientHeight

        // 计算基于比例的尺寸
        const unit = Math.min(
            parentWidth / this.aspectRatio.width,
            parentHeight / this.aspectRatio.height,
        )

        // 设置canvas尺寸
        this.width = canvas.width = unit * this.aspectRatio.width
        this.height = canvas.height = unit * this.aspectRatio.height

        // 计算缩放比例
        const widthRatio = this.width / oldWidth
        const heightRatio = this.height / oldHeight

        // 更新玩家位置和属性
        for (let player of this.players) {
            player.info.x *= widthRatio
            player.info.y *= heightRatio

            player.info.radius = this.height * this.sizeConfig.playerRadius
            player.info.speed = this.height * this.sizeConfig.playerSpeed

            player.width = this.width
            player.height = this.height
        }

        // 更新其他游戏对象
        const gameObjects = GameObject.get_objects()
        for (let obj of gameObjects) {
            if (obj instanceof PlayerBase) continue
            if (typeof obj.resize === 'function' && obj !== this) {
                obj.resize(widthRatio, heightRatio)
            }
        }

        this.draw()
    }

    // 生成一个随机颜色
    get_random_color() {
        let colors = ['red', 'blue', 'green', 'yellow', 'orange', 'purple', 'pink', 'brown', 'gray']
        return colors[Math.floor(Math.random() * colors.length)]
    }

    // 将玩家从玩家列表中移除
    remove_player(player) {
        const index = this.players.indexOf(player)
        if (index !== -1) {
            this.players.splice(index, 1)
        }
    }

    // 初始化玩家（由子类实现）
    initialize_players() {
    }

    start() {
    }

    update() {
        this.draw()
    }

    // 绘制战斗背景
    draw_arena_background() {
        this.ctx.fillStyle = 'rgba(0,0,0,0.2)'
        this.ctx.fillRect(0, 0, this.width, this.height)
    }

    // 显示战斗场地的尺寸
    show_arena_size() {
        // 在左上角显示canvas尺寸
        this.ctx.font = '16px Consolas'
        this.ctx.fillStyle = 'white'
        this.ctx.textAlign = 'left'
        this.ctx.fillText(`W:${this.width} H:${this.height}`, 10, 20)
    }

    draw() {
        this.draw_arena_background()
        this.show_arena_size()
    }
}
